<template>
    <el-aside :width="width">
        <!-- 为什么写在style里就不生效，写在标签里就生效？ -->
        <el-menu text-color="#fff" 
        :collapse="isCollapse" 
        :collapse-transition="false" 
        :default-active="activeMenu" 
        >
        <!-- default-active激活当前tag即高亮 -->
            <h3 v-show="!isCollapse">图书借阅系统</h3>
            <h3 v-show="isCollapse">系统</h3>
            <el-menu-item 
                v-for="item in noChildren" 
                :key="item.path" 
                :index="item.path" 
                @click="handleMenu(item)"
                >
                <component class="icons" :is="item.icon" />
                <span>{{ item.label }}</span>
            </el-menu-item>
            <el-sub-menu v-for="item in hasChildren" :key="item.path" :index="item.path">
                <template #title>
                    <component class="icons" :is="item.icon" />
                    <span>{{ item.label }}</span>
                </template>
                <el-menu-item-group>
                    <el-menu-item 
                        v-for="(subItem,subIndex) in item.children" 
                        :key="subItem.path" 
                        :index="subItem.path"
                        @click="handleMenu(subItem)"
                        >
                        <component class="icons" :is="subItem.icon" />
                        <span>{{ subItem.label }}</span>
                    </el-menu-item>
                </el-menu-item-group>
            </el-sub-menu>
      </el-menu>
    </el-aside>
</template>

<script setup>
import { ref,computed } from 'vue'
import { useAllDataStore } from '@/stores'
import { useRouter,useRoute } from 'vue-router';


const list = computed(() => store.state.menuList)

//list遍历要加value
const noChildren = computed(() => JSON.parse(list.value).filter(item => !item.children))
const hasChildren = computed(() => JSON.parse(list.value).filter(item => item.children))
const store = useAllDataStore()
const isCollapse = computed(() => store.state.isCollapse)
//wideth
const width = computed(() => store.state.isCollapse ? '64px' : '180px')
const router = useRouter()
const route = useRoute()
const handleMenu = (item) => {
    router.push(item.path)
    //更新tag
    store.selectMenu(item)
}

const activeMenu = computed(() => route.path)
</script>

<!-- 这里的scoped很迷 -->
<style lang="less" >
.icons {
    font-size: 18px;
    height:18px;
    margin-right: 5px;
}
.el-menu{
    border-right: 'none';
    background-color: #304156;
    h3{
        line-height:48px;
        color:#fff;
        text-align: center;
    }
}
.el-aside{
    height: 100%;
    background-color: #304156;
}


</style>